package edu.uoc.tdp.servidor;

import edu.uoc.tdp.servidor.*;
import i18n.*;
import edu.uoc.tdp.servidor.LogWriter;
import edu.uoc.tdp.client.comu.Llenguatge;
import edu.uoc.tdp.comu.interficieRMI.InterficieRMI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataOutputStream;
import java.net.MalformedURLException;
import java.net.Socket;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.Locale;
import javax.swing.*;

public class Servidor  extends JFrame{
    
	private static final long serialVersionUID = 1L;
    private JPanel jContentPane;
    private JButton botoArrancar;
    private JButton botoParar;
    private JPanel panellInformacio;
    private JLabel etiquetaPanell;
    private ImplRMI objtrmi;
    LogWriter LogWriter = new LogWriter();

	 public Servidor(){
		 jContentPane = null;
	     botoArrancar = null;
	     botoParar = null;
	     panellInformacio = null;
	     etiquetaPanell = null;
	     initialize();
	}
		
	 private void initialize()
	    {
	        setSize(487, 196);
	        setLocation(new Point(320, 100));
	        setResizable(false);
	        String s = Llenguatge.getMessage("servidor.frame.titol");
	        setTitle(s);
	        setContentPane(getJContentPane());
	    }
    
    
	 public static void main(String args[])
	    {
		    if(args.length == 0)
	            Llenguatge.setDefaultLanguage("i18n/messages");
	        if(args.length == 1)
	        {
	            Locale locale = new Locale(args[0]);
	            Llenguatge.setLanguage("i18n/messages", locale);
	        }
	      
	      SwingUtilities.invokeLater(new Runnable() {

	            public void run()
	            {
	                Servidor servidorPac3 = new Servidor();
	                servidorPac3.setDefaultCloseOperation(3);
	                servidorPac3.setVisible(true);
	            }

	        });
	    }    
	    


	 private JPanel getJContentPane()
	    {
	        if(jContentPane == null)
	        {
	            jContentPane = new JPanel();
	            jContentPane.setLayout(null);
	            jContentPane.add(getBotonArrancar(), null);
	            jContentPane.add(getBotoParar(), null);
	            jContentPane.add(getPanellInformacio(), null);
	        }
	        return jContentPane;
	    }
	 
	 private JButton getBotonArrancar()
	    {
	        if(botoArrancar == null)
	        {
	            botoArrancar = new JButton();
	            botoArrancar.setBounds(new Rectangle(27, 67, 202, 70));
	            botoArrancar.setText(Llenguatge.getMessage("servidor.boto1.titol"));
	            botoArrancar.addActionListener(new ActionListener() {

	                public void actionPerformed(ActionEvent actionevent)
	                {
	                    try
	                    {	
	                    	conectarRemot(true);
	                    }
	                    //catch(Excepcions exception)                  
	                    //{
	                    //	exception.printStackTrace();
	                //    }
	                    catch (ExcepcionsSQLRemota e) {
	                    	LogWriter.setOut(System.out); 
	                       	LogWriter.EscriuLinea(Llenguatge.getMessage("ERR_REMOTO"));
						//	e.printStackTrace();
						}
	                }
           
	            {
	            }
	            });
	        }
	        return botoArrancar;
	    } 
	 
	 public void conectarRemot(boolean flag) throws ExcepcionsSQLRemota //Excepcions
    {
        if(flag)
            try
            {
            	System.out.println("Inici Servidor RMI");
            	InterficieRMI objtrmi = new ImplRMI();
            	//ImplRMI objtrmi = new ImplRMI();
                Naming.rebind("//localhost/gestor", objtrmi );
                botoArrancar.setEnabled(false);
                botoParar.setEnabled(true);
                etiquetaPanell.setForeground(new Color(255, 0, 0));
                etiquetaPanell.setText(Llenguatge.getMessage("servidor.etiqueta2.text"));
            }
            catch(MalformedURLException malformedurlexception)
            {
            	LogWriter.setOut(System.out); 
               	LogWriter.EscriuLinea(Llenguatge.getMessage("ERR_URL"));
                //throw new Excepcions(Excepcions.ERR_URL);
                
            }
            catch(RemoteException remoteexception)
            {               
            	LogWriter.setOut(System.out); 
               	LogWriter.EscriuLinea(Llenguatge.getMessage("ERR_REMOTO"));
                //throw new Excepcions(Excepcions.ERR_REMOTO);
            }
        else
            try
            {
                objtrmi = null;
                Naming.unbind("gestor");
                botoArrancar.setEnabled(true);
                botoParar.setEnabled(false);
                etiquetaPanell.setForeground(new Color(0, 0, 255));
                etiquetaPanell.setText(Llenguatge.getMessage("servidor.etiqueta1.text"));
            }
            catch(RemoteException remoteexception1)
            {
            	LogWriter.setOut(System.out); 
               	LogWriter.EscriuLinea(Llenguatge.getMessage("ERR_REMOTO"));
            	//throw new Excepcions(Excepcions.ERR_REMOTO);
            }
            catch(NotBoundException notboundexception)
            {
            	LogWriter.setOut(System.out); 
               	LogWriter.EscriuLinea(Llenguatge.getMessage("ERR_NOT_BOUND"));
            	//throw new Excepcions(Excepcions.ERR_NOT_BOUND);
            }
            catch(MalformedURLException malformedurlexception1)
            {
            	LogWriter.setOut(System.out); 
               	LogWriter.EscriuLinea(Llenguatge.getMessage("ERR_URL"));
            	//throw new Excepcions(Excepcions.ERR_URL);
            }
    }
	
	 private JButton getBotoParar()
	    {
	        if(botoParar == null)
	        {
	            botoParar = new JButton();
	            botoParar.setBounds(new Rectangle(255, 69, 197, 66));
	            botoParar.setEnabled(false);
	            botoParar.setText(Llenguatge.getMessage("servidor.boto2.titol"));
	            botoParar.addActionListener(new ActionListener() {

	                public void actionPerformed(ActionEvent actionevent)
	                {
	                    try {
	                        conectarRemot(false);
	                    } catch (ExcepcionsSQLRemota e) {
	                    	//e.printStackTrace();
	                    //} catch(Excepcions exception) {
	                        //try {
	                        	LogWriter.setOut(System.out); 
	                           	LogWriter.EscriuLinea(Llenguatge.getMessage("ERR_BD_STOP"));
	                            //throw new Excepcions(Excepcions.ERR_BD_STOP);
	                        //} catch(Excepcions exception1) {
	                          //  exception1.printStackTrace();
	                        //} //catch (ExcepcionsSQLRemota e) {
							//	e.printStackTrace();
							//}
	                      }          
	               }
	           });
	        }
	        return botoParar;
	    }    
	 
	 private JPanel getPanellInformacio()
	    {
	        if(panellInformacio == null)
	        {
	            GridBagConstraints gridbagconstraints = new GridBagConstraints();
	            gridbagconstraints.gridx = 0;
	            gridbagconstraints.gridy = 0;
	            etiquetaPanell = new JLabel();
	            etiquetaPanell.setText(Llenguatge.getMessage("servidor.etiqueta1.text"));
	            etiquetaPanell.setForeground(new Color(0, 0, 255));
	            etiquetaPanell.setDisplayedMnemonic(0);
	            panellInformacio = new JPanel();
	            panellInformacio.setLayout(new GridBagLayout());
	            panellInformacio.setBounds(new Rectangle(27, 15, 425, 40));
	            panellInformacio.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder(null, Llenguatge.getMessage("servidor.panell1.titol"), 0, 0, new Font("Dialog", 1, 12), new Color(51, 51, 51)), null), null));
	            panellInformacio.add(etiquetaPanell, gridbagconstraints);
	        }
	        return panellInformacio;
	    }
	 
}

